﻿#pragma once
#include "yzrutil.h"

namespace yzrilyzr_util{
	template<typename V> EBCLASS(Pool){
			private:
			ArrayList<V>* pool = new ArrayList<V>();
			LinkedList<V>* freed = new LinkedList<V>();
			public:
			ArrayList<V>* getPool() {
				return pool;
			}
			V* obtain() {
				V *v=freed->poll();
				if(v==nullptr) v=newInstance();
				else onReuse(v);
				pool->add(v);
				return v;
				return nullptr;
			}
			void free(V* v) {
				pool->remove(v);
				freed->add(v);
			}
			void free(V* v, Iterator<V>* iter) {
				iter->remove();
				freed->add(v);
			}
			void freeAll() {
				for(u_index i=0;i<pool->size();i++){
					freed->add(pool->get(i));
				}
				pool->clear();
			}
			void clear() {
				pool->clear();
				freed->clear();
			}
			protected:
			virtual V* newInstance() = 0;
			virtual void onReuse(V* v) = 0;
	};
}